package com.cdu.ai_intelligent_teaching_and_training_system.controller;

import com.cdu.ai_intelligent_teaching_and_training_system.dto.ApiResponse;
import com.cdu.ai_intelligent_teaching_and_training_system.entity.Resource;
import com.cdu.ai_intelligent_teaching_and_training_system.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/resources")
@CrossOrigin(origins = "*")
public class ResourceController {
    
    @Autowired
    private ResourceService resourceService;
    
    @GetMapping
    public ResponseEntity<ApiResponse<List<Resource>>> getAllResources() {
        try {
            List<Resource> resources = resourceService.findAll();
            return ResponseEntity.ok(ApiResponse.success(resources));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取资源列表失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Resource>> getResourceById(@PathVariable Long id) {
        try {
            Resource resource = resourceService.findById(id).orElse(null);
            if (resource == null) {
                return ResponseEntity.ok(ApiResponse.error("资源不存在"));
            }
            return ResponseEntity.ok(ApiResponse.success(resource));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取资源详情失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/course/{courseId}")
    public ResponseEntity<ApiResponse<List<Resource>>> getResourcesByCourse(@PathVariable Long courseId) {
        try {
            List<Resource> resources = resourceService.findByCourseId(courseId);
            return ResponseEntity.ok(ApiResponse.success(resources));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取课程资源失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/type/{type}")
    public ResponseEntity<ApiResponse<List<Resource>>> getResourcesByType(@PathVariable String type) {
        try {
            List<Resource> resources = resourceService.findByType(type);
            return ResponseEntity.ok(ApiResponse.success(resources));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取资源失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<Resource>>> searchResources(@RequestParam String title) {
        try {
            List<Resource> resources = resourceService.findByTitleContaining(title);
            return ResponseEntity.ok(ApiResponse.success(resources));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("搜索资源失败：" + e.getMessage()));
        }
    }
    
    @PostMapping
    public ResponseEntity<ApiResponse<Resource>> createResource(@Valid @RequestBody Resource resource) {
        try {
            Resource savedResource = resourceService.save(resource);
            return ResponseEntity.ok(ApiResponse.success("资源创建成功", savedResource));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("创建资源失败：" + e.getMessage()));
        }
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Resource>> updateResource(@PathVariable Long id, @Valid @RequestBody Resource resource) {
        try {
            Resource existingResource = resourceService.findById(id).orElse(null);
            if (existingResource == null) {
                return ResponseEntity.ok(ApiResponse.error("资源不存在"));
            }
            
            resource.setResourceId(id);
            Resource updatedResource = resourceService.save(resource);
            return ResponseEntity.ok(ApiResponse.success("资源更新成功", updatedResource));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("更新资源失败：" + e.getMessage()));
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<String>> deleteResource(@PathVariable Long id) {
        try {
            Resource existingResource = resourceService.findById(id).orElse(null);
            if (existingResource == null) {
                return ResponseEntity.ok(ApiResponse.error("资源不存在"));
            }
            
            resourceService.deleteById(id);
            return ResponseEntity.ok(ApiResponse.success("资源删除成功", null));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("删除资源失败：" + e.getMessage()));
        }
    }
} 